package pers.zh.parking.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.RxLifecycleUtils;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import pers.zh.parking.common.BaseObserver;
import pers.zh.parking.common.BaseResponse;
import pers.zh.parking.common.RxScheduler;
import pers.zh.parking.data.entity.User;
import pers.zh.parking.mvp.contract.SignupContract;
import pers.zjc.commonlibs.util.RegexUtils;
import pers.zjc.commonlibs.util.StringUtils;

@ActivityScope
public class SignupPresenter extends BasePresenter<SignupContract.Model, SignupContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SignupPresenter(SignupContract.Model model, SignupContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void signUp(String account, String password, String userName) {
        if (StringUtils.isEmpty(account)) {
            mRootView.showMessage("请输入账号");
            return;
        }
        if (StringUtils.isEmpty(password)) {
            mRootView.showMessage("请输入密码");
            return;
        }
        if (StringUtils.isEmpty(userName)) {
            mRootView.showMessage("请输入用户名");
            return;
        }
        if (userName.length() > 5) {
            mRootView.showMessage("用户名过长，最多5位");
            return;
        }
        if (password.length() < 6) {
            mRootView.showMessage("密码过短，至少6位");
            return;
        }
        User user = new User();
        user.setAccount(account);
        user.setPassword(password);
        user.setUserName(userName);
        mModel.signUp(user)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse baseResponse) {
                      mRootView.signUpSuccess();
                  }
              });
    }
}
